<template>
  <div>
    <el-form
      ref="query"
      :model="query"
      :inline="true"
      label-width="80px"
      size="mini"
    >
      <el-form-item label="图书编号">
        <el-input v-model="query.book_num" style="width: 120px"></el-input>
      </el-form-item>
      <el-form-item label="图书名称">
        <el-input v-model="query.book_name" style="width: 120px"></el-input>
      </el-form-item>
      <el-form-item label="作者">
        <el-input v-model="query.writer" style="width: 120px"></el-input>
      </el-form-item>
      <el-form-item label="出版社">
        <el-input v-model="query.publisher" style="width: 120px"></el-input>
      </el-form-item>
      <el-form-item label="所在位置">
        <el-input v-model="query.site" style="width: 120px"></el-input>
      </el-form-item>
      <el-form-item label="可否借阅">
        <el-input v-model="query.isBorrow" style="width: 120px"></el-input>
      </el-form-item>
    </el-form>

    <div class="btn">
      <div class="right">
        <el-button
          @click="bookQuery"
          size="mini"
          type="primary"
          icon="el-icon-search"
          >查询</el-button
        >
        <el-button @click="resetForm" size="mini" icon="el-icon-refresh"
          >重置</el-button
        >
      </div>
    </div>

    <el-table :data="tableData" border size="mini">
      <el-table-column
        type="index"
        :index="indexMethod"
        label="序号"
        align="center"
      >
      </el-table-column>
      <el-table-column prop="book_num" label="图书编号" width="180">
      </el-table-column>
      <el-table-column prop="book_name" label="图书名称" width="140">
      </el-table-column>
      <el-table-column prop="writer" label="作者" width="90"> </el-table-column>
      <el-table-column prop="publisher" label="出版社" width="150">
      </el-table-column>
      <el-table-column prop="price" label="价格" width="90"> </el-table-column>
      <el-table-column prop="site" label="所在位置" width="90">
      </el-table-column>
      <el-table-column prop="amount" label="图书数量" width="70">
      </el-table-column>
      <el-table-column label="可否借阅" width="95">
        <template v-slot="scope">
          <el-button
            size="mini"
            type="success"
            plain
            v-if="scope.row.amount > 0"
            @click="borrowBook(scope.$index, scope.row)"
            >{{ "可借阅" }}</el-button
          >
          <el-button
            size="mini"
            type="danger"
            plain
            v-if="scope.row.amount == 0"
            @click="notBorrowBook(scope.$index, scope.row)"
            >{{ "不可借阅" }}</el-button
          >
        </template>
      </el-table-column>
      <el-table-column type="expand" label="图书简介" width="100">
        <template v-slot="props">
          <el-form label-position="right" inline class="demo-table-expand">
            <el-form-item>
              <span>{{ props.row.introduction }}</span>
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>
    </el-table>

    <div style="margin-top: 10px">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="1"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="10"
        layout="total, sizes, prev, pager, next, jumper"
        :total="tableDataTotal"
      ></el-pagination>
    </div>

    <el-dialog title="提示" width="30%" v-model="isHaveAmount">
      <div>图书【{{ bookName }}】没有库存，无法借阅!!</div>
      <div style="text-align: right">
        <el-button size="mini" type="primary" @click="isHaveAmount = false"
          >确 定</el-button
        >
      </div>
    </el-dialog>

    <el-dialog title="请输入借阅时长" width="30%" v-model="borrowTimeVisible">
      <el-form
        ref="borrowForm"
        :model="borrowForm"
        :inline="true"
        label-width="80px"
        size="mini"
      >
        <el-form-item>
          <el-input
            v-model="borrowForm.borrow_time"
            placeholder="最大借阅时长30天"
          ></el-input>
        </el-form-item>
      </el-form>
      <div style="text-align: right">
        <el-button size="mini" @click="borrowTimeVisible = false"
          >取 消</el-button
        >
        <el-button size="mini" type="primary" @click="borrowSubmit"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { page } from "../../api/book/book";
import { borrowBook as borrow} from "../../api/book/borrow";
export default {
  data() {
    return {
      tableData: [],
      query: { page: 1, limit: 10 },
      tableDataTotal: 0,
      isHaveAmount: false,
      bookName: "",
      borrowTimeVisible: false,
      borrowForm: {},
    };
  },
  created() {
    this.getPage();
  },
  methods: {
    indexMethod(index) {
      return (this.query.page - 1) * this.query.limit + (index + 1);
    },
    getPage() {
      page(this.query).then((res) => {
        console.log(res);
        this.tableData = res.data.rows;
        this.tableDataTotal = res.data.count;
      });
    },
    handleSizeChange(val) {
      this.query.limit = val;
      this.getPage();
    },
    handleCurrentChange(val) {
      this.query.page = val;
      this.getPage();
    },
    bookQuery() {
      this.getPage();
    },
    resetForm() {
      this.query = { page: 1, limit: 10 };
      this.getPage();
    },
    borrowBook(index, row) {
      this.borrowForm = {}
      this.borrowTimeVisible = true;
      // console.log(row);
      this.borrowForm.username = row.username
      this.borrowForm.book_num = row.book_num
      this.borrowForm.book_name = row.book_name
      this.borrowForm.amount = row.amount
    },
    notBorrowBook(index, row) {
      this.isHaveAmount = true;
      this.bookName = row.book_name;
    },
    borrowSubmit() {
      this.borrowForm.borrow_time = parseInt(this.borrowForm.borrow_time);
      borrow(this.borrowForm)
        .then((res) => {
          if (res.success) {
            this.$message({ message: `${res.data}`, type: "success" });
            this.borrowTimeVisible = false;
            this.getPage()
          } else {
            this.$message.error(res.data);
             this.borrowTimeVisible = false;
          }
        })
        .catch(() => {
          this.$message.error("借阅图书失败!");
          this.borrowTimeVisible = false;
        });

    },
  },
};
</script>
<style lang="scss" scoped>
</style>